package cn.pn.project.controller;


import cn.pn.project.client.base.CityFeign;
import cn.pn.project.client.commodity.CommodityFeign;
import cn.pn.project.client.commodity.ShowtypeFeign;
import cn.pn.project.common.GetDate;
import cn.pn.project.common.Page;
import cn.pn.project.module.commodity.CommodityModule;
import cn.pn.project.pojo.base.City;
import cn.pn.project.pojo.commodity.Commodity;
import cn.pn.project.pojo.commodity.Showtype;
import cn.pn.project.vo.CommodityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dis/p")
public class ItemDisController {
    @Autowired
    private CommodityFeign commodityFeign;
    @Autowired
    private ShowtypeFeign showtypeFeign;

    @Autowired
    private CityFeign cityFeign;



    /**
     * 根据商品id查询出商品详情所有信息
     * @param commodityId
     * @return
     */
        @PostMapping("/findCommodityAllMessage")
        public Object findCommodityAllMessage(@RequestParam("commodityId") Integer commodityId){
        //查询商品主要信息
        System.out.println("-------------------------------------");
        CommodityModule commodityModule=commodityFeign.findCommodityAllMessage(commodityId);
        //查询商品须知
        //查询商品票档
        //查询商品场次

        return commodityModule;
    }





    /**
     *
     *折扣商品不分页
     * cityId  城市Id 不能为空
     * statusId 0 首页，1更多
     * 这里我直接定死 首页加载时，显示前6条数据
     * 点击更多时，显示前20条
     *
     *
     *
     */
    @RequestMapping(value = "/getDisCommodityListByCityId", method = RequestMethod.POST)
    public List<Commodity> getDisCommodityListByCityId(@RequestParam("cityId") Integer cityId,@RequestParam("statusId") Integer statusId)
    {

        System.out.println("-----------------------111111");

        Map<String,Object> one=new HashMap<>();
        Page<Commodity> page=null;
        if(statusId==0){
            //数据量较少，暂定显示1条
            page =new Page<>(0,6,6);
        }else{
            page =new Page<>(0,20,20);
        }
        //当前页码
        one.put("page",page);
        one.put("cityId",cityId);
        one.put("statusId",statusId);
        List<Commodity> list=commodityFeign.getDisCommodityListByCityId(one);
        return list;
        //return commodityFeign.getDisCommodityListByCityId(one);
        //return "successCallback2("+ JSONObject.toJSONString(DtoUtil.returnDataSuccess(commodityFeign.getDisCommodityListByCityId(one)))+")";
    }

    /**
     *
     *热门商品不分页
     * cityId  城市Id 不能为空
     * statusId 0 首页，1更多
     * 这里我直接定死 首页加载时，显示前6条数据
     * 点击更多时，显示前20条
     *
     *
     *
     */

    @RequestMapping(value = "/getHotCommodityListByCityId",method = RequestMethod.POST)
    public List<Commodity> getHotCommodityListByCityId(@RequestParam("cityId") Integer cityId,@RequestParam("statusId") Integer statusId){
        System.out.println("-----------------------222222");

        Map<String,Object> one=new HashMap<>();
        Page<Commodity> page=null;
        if(statusId==0){
            //数据量较少，暂定显示1条
            page =new Page<>(0,6,6);
        }else{
            page =new Page<>(0,20,20);
        }
        //当前页码
        one.put("page",page);
        one.put("cityId",cityId);
        one.put("statusId",statusId);
        List<Commodity> list=commodityFeign.getHotCommodityListByCityId(one);
        System.out.println(list.size()+"      -----------");
        return list;
    }

    /**
     * 根据商品Id 查询商品排期
     * @param commodityId
     * @return
     */
    @RequestMapping(value = "/getCommodityScheduleByCommodityId", method = RequestMethod.POST)
    public List<Commodity> getCommodityScheduleByCommodityId(@RequestParam("commodityId")Integer commodityId){
        return commodityFeign.getCommodityScheduleByCommodityId(commodityId);
    }

    /**
     * 首页面加载时默认只查询前6条
     * 首页点击更多时， 根据父级类型和城市进行分页查
     * @param fatherTypeId 父类类型
     * @param cityId   城市编号
     *  @param  statusId  状态编号,0为默认，即首页面加载时默认只查询前6条 1 开始进行分页查询
     * @return
     * -----------------------------------------------------------------------------------------------------
     */
   @RequestMapping(value = "/getCommodityListByCityIdAndFatherTypeId", method = RequestMethod.POST)
    public Page<Commodity> getCommodityListByCityIdAndFatherTypeId(@RequestParam("fatherTypeId") Integer fatherTypeId,@RequestParam("cityId") Integer cityId,@RequestParam("statusId") Integer statusId,@RequestParam("pageNo") Integer pageNo){

       if(pageNo<=0){
           pageNo=1;
       }
       //获取总数
       Map<String,Object> one=new HashMap<>();

       Integer count =commodityFeign.getCommodityCountByCityIdAndFatherTypeId(fatherTypeId,cityId).size();


       System.out.println("数据总量  ===》 "+count);
       Page<Commodity> page=null;
       if(statusId==0){
           //数据量较少，暂定显示1条
           page =new Page<>(1,6,count);
       }else{
           page =new Page<>(pageNo,1,count);
           System.out.println("总页数===  》》》"+page.getPageCount());

       }

       one.put("page",page);
       one.put("fatherTypeId",fatherTypeId);
       one.put("cityId",cityId);
       one.put("statusId",statusId);
       one.put("pageNo",pageNo);
       List<Commodity> list=commodityFeign.getCommodityListByCityIdAndFatherTypeId(one);
       page.setRows(list);
       return page;
    }

    /**
     * 根据父类类型获取商品集合，并循环展示在页面
     * @param      //Map<String,List<Commodity>> getCommodityMapByFatherList(@RequestParam("cityId")Integer cityId){//
     * @return
     * ------------------8/8    这里可能会有问题，因为点击更多会进行分页显示，所以要考虑传入当前页码，和每页显示数量
     */
    /*@RequestMapping(value = "/getCommodityMapByFatherList", method = RequestMethod.POST)
    public Map<Integer,List<Commodity>> getCommodityMapByFatherList(@RequestParam("cityId")Integer cityId,@RequestParam("statusId")Integer statusId){//

        //@RequestBody Map<String,Object> one
        Map<Integer,List<Commodity>> CommodityMapByShowtype=new HashMap<>();
        //获得全部父级类型
        List<Showtype> showtypeList= showtypeFeign.listAllType();
        for (Showtype type:showtypeList){
            System.out.println(type.getShowTypeId()+"   "+type.getTypeName()+"   "+type.getShowTypeId());
            Integer fatherId=type.getShowTypeId();
            //通过父级Id和城市Id获取商品集合，首页加载的时候可以定位基数一个样式，偶数一个样式，看情况而定

            Map<String,Object> cityFatherIdPageMap=new HashMap<>();
            cityFatherIdPageMap.put("fatherTypeId",fatherId);
            cityFatherIdPageMap.put("cityId",cityId);

            Integer count =commodityFeign.getCommodityCountByCityIdAndFatherTypeId(fatherId,cityId).size();
            System.out.println("集合总数====》  "+count);

            Page<Commodity> page=null;
            if(count>0){
                 page=new Page<>(1,6,20);
                cityFatherIdPageMap.put("page",page);//
                List<Commodity> commodityList=commodityFeign.getCommodityListByCityIdAndFatherTypeId(cityFatherIdPageMap);
                CommodityMapByShowtype.put(type.getShowTypeId(),commodityList);//这里在做的时候要考虑键为对象，因为会点击更多，需要类型Id
            }

        }
        return CommodityMapByShowtype;
    }*/


    /**
     * 根据父类类型查询商品集合
     * @param cityId
     * @param statusId
     * @param fatherTypeId
     * @return
     */
    @RequestMapping(value = "/getCommodityMapByFatherList", method = RequestMethod.POST)
    public List<Commodity> getCommodityMapByFatherList(@RequestParam("cityId")Integer cityId,@RequestParam("statusId")Integer statusId,@RequestParam("fatherTypeId")Integer fatherTypeId){//

        List<Commodity> commodityList=null;

        Map<String,Object> cityFatherIdPageMap=new HashMap<>();
        cityFatherIdPageMap.put("fatherTypeId",fatherTypeId);
        cityFatherIdPageMap.put("cityId",cityId);

        Integer count =commodityFeign.getCommodityCountByCityIdAndFatherTypeId(fatherTypeId,cityId).size();
        System.out.println("集合总数====》  "+count);

        Page<Commodity> page=null;
        if(count>0){
            page=new Page<>(1,6,20);
            cityFatherIdPageMap.put("page",page);//
            commodityList=commodityFeign.getCommodityListByCityIdAndFatherTypeId(cityFatherIdPageMap);

            System.out.println(commodityList.size()+"       --------------------***************++++++++++");
        }else{
            commodityList=new ArrayList<>();
        }

        return commodityList;
    }


    /**
     * 多条件查询商品信息
     * @param
     * @return
     * map集合封装了 commodityName，cityId，typeId，clildTypeId
     * dateStatus 默认为0，查询大于此时此刻的全部时间段
     * dateStatus(日期状态)，为1或2时 只需要一个 futureTime
     * dateStatus为3或4时 beforeTime，futureTime，
     * orderStatus 排序方式，默认为0，不能为空，其他的1 按上架时间降序，2 按价格升序 3. 按好评降序
     * zhiDingDate 前台传过来的指定日期
     * isSeat 是否支持在线选座 0不支持  1支持
     * pageNo 分页页码
     */
   /* public Page<Commodity> getCommodityByMoreReason(@RequestBody Map<String,Object> one)*/

    @RequestMapping(value = "/getCommodityByMoreReason", method = RequestMethod.POST)
    public Page<Commodity> getCommodityByMoreReason(@RequestParam(value = "commodityName",required = false) String  commodityName,
                                                    @RequestParam(value = "cityId",required = false) Integer  cityId,//城市也必须要有
                                                    @RequestParam(value = "typeId",required = false) Integer  typeId,
                                                     @RequestParam(value = "clildTypeId",required = false) Integer  clildTypeId,

                                                    @RequestParam(value = "dateStatus",required = false) Integer  dateStatus,//日期状态，必须要有
                                                    @RequestParam(value = "zhiDingDate",required = false) String  zhiDingDate,//指定日期

                                                    @RequestParam(value = "orderStatus",required = false) Integer  orderStatus,//必须要有，排序方式
                                                    @RequestParam(value = "isSeat",required = false) Integer  isSeat,//必须要有，是否支持分页
                                                    @RequestParam(value = "pageNo",required = false) Integer  pageNo//分页页码，当前页码

                                                    ){

        //接下来应该封住为Map集合

        System.out.println(commodityName+"   -------商品名");

        System.out.println(cityId+"   ------城市名");
        Map<String,Object> one=new HashMap<>();
        one.put("commodityName",commodityName);
        one.put("cityId",cityId);
        one.put("typeId",typeId);
        one.put("clildTypeId",clildTypeId);
        one.put("orderStatus",orderStatus);
        one.put("isSeat",isSeat);
        //获取当前日期到本周末24时时间段内的场次
        if(dateStatus==1){
            //调用获取下个礼拜一日期的方法
            String nextMon= GetDate.getNextMon();
            one.put("futureTime",nextMon);
        }
        //获取当前日期到本月末24时时间段内的场次
        if(dateStatus==2){
            //调用获取下个月一号方法
            String nextMonth= GetDate.getNextMonth();
            one.put("futureTime",nextMonth);
        }
        //获取本周末凌晨到24时时间段内的商品信息
        if(dateStatus==3){
            //获取当前周末所在日期
            String currentWeek=GetDate.getCurrentWeek();
            //获取前一天日期
            String currentDateBefore=GetDate.getCurrentDateBefore(currentWeek);
            //获取后一天的日期
            String currentDateAfter=GetDate.getCurrentDateAfter(currentWeek);
            one.put("beforeTime",currentDateBefore);
            one.put("futureTime",currentDateAfter);
        }
        //获取指定日期凌晨到24时时间段内的商品信息
        if(dateStatus==4){

            //获取前一天日期
            String currentDateBefore=GetDate.getCurrentDateBefore(zhiDingDate);
            //获取后一天的日期
            String currentDateAfter=GetDate.getCurrentDateAfter(zhiDingDate);
            one.put("beforeTime",currentDateBefore);
            one.put("futureTime",currentDateAfter);
        }


        System.out.println("   ----------  pageNo  "+pageNo);
        if(pageNo<=0){
            pageNo=1;
        }

        Integer count =commodityFeign.getCountCommodityByMoreReason(one).size();


        System.out.println("数据总量  ===》 "+count);
        Page<Commodity> page =new Page<>(pageNo,3,count);
        one.put("page",page);
        List<Commodity> list=commodityFeign.getCommodityByMoreReason(one);
        page.setRows(list);

        System.out.println(page.getPageCount()+"  -----》总页数");

        //获取总数

        return page;

    }


    @RequestMapping(value = "/getCommodityByCommodityIdAndCommodityStatus", method = RequestMethod.POST)
    public List<CommodityVo> getCommodityByCommodityIdAndCommodityStatus(@RequestBody Map<String,Object> one){
        return commodityFeign.getCommodityByCommodityIdAndCommodityStatus(one);
    }

    /**
     * 根据商品id 查询商品信息，---------cp
     * ----------------------------------张夏青用
     * @param commodityId
     * @return
     */
    @RequestMapping(value = "/getCommodityByCommodityId", method = RequestMethod.GET)
    public Commodity getCommodityByCommodityId(@RequestParam("commodityId") Integer commodityId){
        return commodityFeign.getCommodityByCommodityId(commodityId);
    }

    @RequestMapping(value = "/getCityNameByCityId", method = RequestMethod.POST)
    public City getCityNameByCityId(@RequestParam("cityId") Integer cityId) throws Exception{
        return cityFeign.getCityById(cityId);
    }


    @RequestMapping(value = "/DuoTiaoJianChaXun", method = RequestMethod.POST)
    public City DuoTiaoJianChaXun(@RequestParam("cityId") Integer cityId) throws Exception{
        return cityFeign.getCityById(cityId);
    }


}
